package com.idea.zd.sys.train.thread;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test implements Runnable{
    A a = new A();
    B b = new B();
    public static void main(String [] args){

        System.out.println(5.3%2);

        /*Test test = new Test();
        new Thread(test).start();
        test.init();*/

        /*List<Map<String,String>> list = new ArrayList<>();

        Map<String,String> map = new HashMap<>();
        map.put("String","String");
        list.add(map);
        System.out.print(list);
        map.put("sss","sss");
        list.add(map);
        System.out.print(list);*/

    }

    public void init(){
        Thread.currentThread().setName("主线程");
        a.foo(b);
    }

    @Override
    public void run() {

        Thread.currentThread().setName("副线程");
        b.bar(a);

    }
}

class A{
    public synchronized void foo(B b){
        System.out.println("当前线程："+Thread.currentThread().getName());

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("试图进入B的last");
        b.last();
    }

    public synchronized void last(){
        System.out.println("进入A的last");
    }
}

class B{

    public synchronized void bar(A a){
        System.out.println("当前线程："+Thread.currentThread().getName());

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("试图进入A的last");
        a.last();
    }
    public synchronized void last(){
        System.out.println("进入B的last");
    }
}
